<template>
  <div>登录中.............(为了接收授权码的页面)</div>
</template>

<script>
export default {
  data() {
    return {

    }
  },
  methods: {
    //oauth2登录的code码:  ?code=pnl78o&state=normal
    getCode(){
      //码云的参数:?code=9c0bc76148eaec22ad0f74552ecb98a275df1cc87f7520ddf806ba3457092ad3
      var args = location.search;
      // var args = "?code=pnl78o&state=normal";
      var strs = args.split("=");


      if(strs.length<=2){
        //码云登录
        //把授权码发到后台,让后台获取token  ---> 获取用户信息
        this.axios("http://127.0.0.1:8888/api/user/giteeCallback?code="+strs[1]).then(resp=>{
          // localStorage.setItem("token",resp.data.token)
          localStorage.setItem("username",resp.data.name)
          localStorage.setItem("avatar",resp.data.avatar_url)
          this.$router.push("/")
        })
      }else {
        //oauth 登录
        var string = strs[1];//pnl78o&state
        let code = string.split("&")[0] //pnl78o
        this.axios("http://127.0.0.1:8888/api/user/myCallback?code=" + code).then(resp => {

          localStorage.setItem("username", resp.data)
          this.$router.push("/")
        })
      }
    }
  },
  created() {
    this.getCode();
  }
}
</script>

<style scoped>

</style>